package com.litong.rms.dao.impl;
import java.util.List;

import com.litong.rms.bean.User;
import com.litong.rms.dao.UserDao;

public class UserDaoImpl extends BaseDaoImpl implements UserDao{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6223197809480475094L;

	public void addUser(User u) {
		save(u);
	}

	public void deleteUser(User u) {
		//u = findUser(u);
		delete(u);
	}

	public User findUser(User u) {
		String userName = u.getUserName().trim();
		String password = u.getPassword().trim();
		String queryString = "from User where userName = ? and userPassword = ?";
		Object[] values = new String[2];
		values[0] = userName;
		values[1] = password;
		/*List<User> userList = null;
		try {
			//userList = this.getHibernateTemplate().findByExample(u);
			//userList = this.getHibernateTemplate().find(queryString, values);
			//userList = this.getHibernateTemplate().find("from User where userName='"+userName+"' and "+"userPassword='"+password+"'");
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		List<User> userList = this.find(queryString, values);
		
		if(userList!=null&&!userList.isEmpty()){
			User user = userList.get(0);
			return user;
		}else{
			return null;
		}	
	}

	public void updateUser(User u) {
		saveOrUpdate(u);
		
	}

	@SuppressWarnings("unchecked")
	public List<User> getUserList() {
		String queryString = "from User";
		List<User> userList = getEntityList(queryString);
		if(userList!=null&&!userList.isEmpty()){
			return userList;
		}
		return null;
	}

	public User findById(int id) {
		String queryString = "from User where userId = ?";
		int value = id;
		List<User> userList = this.find(queryString, value);
		if(userList!=null&&!userList.isEmpty()){
			User user = userList.get(0);
			return user;
		}else{
			return null;
		}	
	}
		
}
